﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kingdee.WeksErp.DAL;
using Kingdee.WeksErp.Models.Dto;
using Kingdee.WeksErp.Models.Suppliers;

namespace Kingdee.WeksErp.Controllers
{
    public class SupplierController : BaseController
    {
        private WeksErpContext db = new WeksErpContext();


        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>

        public ActionResult List()
        {
            var list = db.Database.SqlQuery<TranspSupplier>("SELECT FItemId as ID, FItemId , FName FROM   dbo.IC_Logistics WHERE FUnUsed=0 ").ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 供应商报价系数
        /// </summary>
        /// <returns></returns>
        public ActionResult CofList()
        {
            int pageSize = Request["rows"] == null ? 20 : int.Parse(Request["rows"]);
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int supplier = 0;
            int.TryParse(Request["supplier"], out supplier);

            var city = Request["city"];
            var area = Request["area"];
            var tmp = db.TranspSupplierCofs.Where(t => t.FItemId == supplier);
            int provinceId = 0;
            int cityId = 0;
            int areaId = 0;
            if (int.TryParse(Request["province"] ?? "", out provinceId))
            {
                if (provinceId != 0)
                {
                    tmp = tmp.Where(t => t.FProvinceId == provinceId);
                }
            }
            if (int.TryParse(Request["city"] ?? "", out cityId))
            {
                if (cityId != 0)
                {
                    tmp = tmp.Where(t => t.FCityId == cityId);
                }
            }

            if (int.TryParse(Request["area"] ?? "", out areaId))
            {
                if (areaId != 0)
                {
                    tmp = tmp.Where(t => t.FCountryId == areaId);
                }
            }
            var name = Request["name"] ?? "";
            if (!string.IsNullOrEmpty(name))
            {
                tmp = tmp.Where(t => t.FCountryName.Contains(name));
            }

            int total = tmp.Count();
            //分页的处理
            var list = tmp.OrderBy(t => t.FProvinceId).ThenBy(t => t.FCityId).ThenBy(t => t.FCountryId).Skip(pageSize * (pageIndex - 1)).Take(pageSize).AsQueryable();

            // var list = db.TranspSupplierCofs.ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public ActionResult ProvinceData()
        {
            var list = new List<ItemInfo>()
            {
                new ItemInfo() {Id = 0, Name = "全部"}
            };
            var temp = db.Database.SqlQuery<ItemInfo>("SELECT  FInterID AS Id ,FName AS Name FROM t_SubMessage WHERE FTypeID=701").ToList();
            list.AddRange(temp);
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

    }
}